package code.lewdy.app.user;

import java.util.Date;
import java.util.Random;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.Session;

import code.lewdy.db.HibernateTemple;
import code.lewdy.util.FormatUtil;

import com.lt.member.Member;
import com.lt.member.MemberService;

import dao.HibernateSessionFactory;

/**账户金额变动记录
 * @author Administrator
 *
 */
@Entity
@Table
public class AccountRecord {
	
	@Transient
	public static final int INCOME = 0; //收入
	@Transient
	public static final int EXPENDITURE = 1; //支出
	
	@Transient
	public static final int GROUP_USER_DEPOSIT = 0;//用户存款
	@Transient
	public static final int GROUP_LOTTERY_BET = 1;//投注彩票
	@Transient
	public static final int GROUP_LOTTERY_AFTER_BET = 11;//追号投注
	@Transient
	public static final int GROUP_LOTTERY_UNION_BET = 12;//合买投注
	@Transient
	public static final int GROUP_LOTTERY_UNION_PAY = 13;//合买派奖
	@Transient
	public static final int GROUP_LOTTERY_BET_CANCEL = 14;//撤单返额
	@Transient
	public static final int GROUP_LOTTERY_UNION_ROYALTY = 15;//跟单提成
	@Transient
	public static final int GROUP_USER_WITHDRAW = 2;//用户提现
	@Transient
	public static final int GROUP_RECHARGE_FOR_OTHER = 3;//为他人充值
	@Transient
	public static final int GROUP_RECHARGE_BY_OTHER = 4;//他人为我充值
	@Transient
	public static final int GROUP_RECHARGE_BY_ONLINE = 41;//在线自动充值
	@Transient
	public static final int GROUP_REWARD_PAY = 5;//派奖
	@Transient
	public static final int GROUP_RETURN = 6;//投注返点
	@Transient
	public static final int GROUP_RETURN_LOWER = 7;//下级消费返点
	@Transient
	public static final int GROUP_AFTER_BET_CANCEL = 8;//追号取消
	@Transient
	public static final int GROUP_RECHARGE_BY_ADMIN = 9;//管理员充值
	
	private Long id;
	private String orderId; //订单号，唯一
	private Long ownerId; //数据拥有人id，记录对象
	private String ownerName;
	private Long attachId;  //事件数据id
	private String attachName; //事件名称
	private int groupId; //账变分组
	private int type;  //type=0，收入(相对于owner来说);type=1表示支出
	private String note; //备注，说明
	private double amountIn; //收入金额
	private double amountOut; //支出金额
	private Double balance; //账户余额
	private Date createDate;
	private int status; //0-正常|1-删除
	private String ext1;//扩展字段1
	private String ext2;//扩展字段2
	private String ext3;//扩展字段3 
	
	/**插入新记录快捷方法
	 * @param m
	 * @param orderId
	 * @param group
	 * @param amount
	 * @param note
	 * @return
	 */
	public AccountRecord insert(Member m, String orderId, int group, double amount, String note){
		if(m == null){
			m = new Member();
			m.setId(-1);
			m.setUsername("未知");
		}
		this.setAmountIn(amount > 0 ? amount : 0);
		this.setAmountOut(amount<0 ? amount : 0);
		this.setBalance(MemberService.getBalance(m.getId()));
		this.setCreateDate(new Date());
		this.setGroupId(group);
		this.setNote(note);
		this.setOrderId(orderId);
		this.setOwnerId(new Long(m.getId()));
		this.setOwnerName(m.getUsername());
		this.setType(this.getAmountIn()>0 ? INCOME : EXPENDITURE);
		id = HibernateTemple.save(this);
		this.setId(id);
		return this;
	}
	/*获取账变类型
	 * @return
	 */
	@Transient
	public String getGroup(){
		switch (this.groupId) {
		case AccountRecord.GROUP_LOTTERY_AFTER_BET: return "追号投注";
		case AccountRecord.GROUP_LOTTERY_BET: return "投注";
		case AccountRecord.GROUP_RECHARGE_BY_OTHER: return "上级充值";
		case AccountRecord.GROUP_RECHARGE_FOR_OTHER: return "为他人充值";
		case AccountRecord.GROUP_RETURN: return "返点";
		case AccountRecord.GROUP_REWARD_PAY: return "中奖金额";
		case AccountRecord.GROUP_USER_DEPOSIT: return "用户充值";
		case AccountRecord.GROUP_USER_WITHDRAW: return "用户提现";
		case AccountRecord.GROUP_RETURN_LOWER: return note;
		case AccountRecord.GROUP_AFTER_BET_CANCEL: return note;
		case AccountRecord.GROUP_RECHARGE_BY_ADMIN: return note;
		default:return note;
		}
	}
	
	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getOwnerId() {
		return ownerId;
	}
	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
	}
	@Column(length=50)
	public String getOwnerName() {
		return ownerName;
	}
	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}
	public Long getAttachId() {
		return attachId;
	}
	public void setAttachId(Long attachId) {
		this.attachId = attachId;
	}
	@Column(length=50)
	public String getAttachName() {
		return attachName;
	}
	public void setAttachName(String attachName) {
		this.attachName = attachName;
	}
	
	public double getAmountIn() {
		return FormatUtil.formatDecimal(amountIn, 4);
	}
	public void setAmountIn(double amountIn) {
		this.amountIn = amountIn;
	}
	public double getAmountOut() {
		return FormatUtil.formatDecimal(amountOut, 4);
	}
	public void setAmountOut(double amountOut) {
		this.amountOut = amountOut;
	}
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	@Column(length=1000)
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public String getExt1() {
		return ext1;
	}
	public void setExt1(String ext1) {
		this.ext1 = ext1;
	}
	public String getExt2() {
		return ext2;
	}
	public void setExt2(String ext2) {
		this.ext2 = ext2;
	}
	public String getExt3() {
		return ext3;
	}
	public void setExt3(String ext3) {
		this.ext3 = ext3;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public int getStatus() {
		return status;
	}
	public int getGroupId() {
		return groupId;
	}
	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getOrderId() {
		if(orderId==null)
			orderId = (new Date().getTime()+new Random().nextInt())+"";
		return orderId;
	}
	
	public static void main(String[] args) {
		Session s = HibernateSessionFactory.getSession();
		s.beginTransaction();
		s.save(new AccountRecord());
		s.getTransaction().commit();
		s.close();
	}

	public void setBalance(Double balance) {
		this.balance = balance;
	}

	public Double getBalance() {
		return FormatUtil.formatDecimal(balance, 4);
	}
	
}
